qui(function (require, exports, module) {
    live2VOD.Util = $.extend(true, {}, EpgWeb.Util, {
        formatter: {
            operate: function (cellvalue, options, rowObject) {
                return '<button type="button" onclick="live2VOD.Util.jumpToEdit(' + rowObject.id + ' )">修改</button>' + "|" + '<button onclick="live2VOD.Util.delChannel(' + rowObject.id + ')">删除</button>'
            },
            channeltypeShow: function (cellvalue, options, rowObject) {

                var rule = /[\u4E00-\u9FA5a-zA-Z0-9]+/g;
                var channeltype = rowObject.channeltype;

                var jsonArray = channeltype.match(rule);
                try {
                    var result = "";
                    $.each(jsonArray, function(i,data) {
                        result = result + data;
                        if (i < jsonArray.length - 1) {
                            result = result + "   ";
                        }
                    });
                    return result;
                } catch (e){return channeltype};
            },
            toUpperCase: function (cellvalue, options, rowObject) {
                return rowObject.ptype.toUpperCase();
            }
        },
        modifyChannel:function(params){
            live2VOD.Dao.modifyChannel(params, function (data) {
                //{"code": 200, "meta": {}, "response": {"msg": "success", "code": "A00000"}}
                if(data != null && data.code ==200 ){
                    if(data.response.code =="A00000" ){
                        EpgWeb.Plugin.showTips({content:"success" });
                        setTimeout(function(){
                            window.location.href = "/epg/live2VOD_cp_home.html"
                        } ,2000)
                    }else{
                        EpgWeb.Plugin.showTips({type:"error",content:data.response.msg })
                    }
                }else{
                    EpgWeb.Plugin.showTips({type:"error",content:"live2VOD modifyChannel has error" })
                }
            })
        },
        addChannel:function(params){
            live2VOD.Dao.addChannel(params, function (data) {
                //{"code": 200, "meta": {}, "response": {"msg": "success", "code": "A00000"}}
                if(data != null && data.code ==200 ){
                    if(data.response.code =="A00000" ){
                        EpgWeb.Plugin.showTips({content:"success" });
                        setTimeout(function(){
                            window.location.href = "/epg/live2VOD_cp_home.html"
                        } ,2000)
                    }else{
                        EpgWeb.Plugin.showTips({type:"error",content:data.response.msg })
                    }
                }else{
                    EpgWeb.Plugin.showTips({type:"error",content:"live2VOD addChannel has error" })
                }
            })
        },
        delChannel: function (id) {
            SystemDialog.queryConfirm("确认要删除么？",function(flag){
                if(flag){
                    if (id != null) {
                        var element = live2VOD.Util.getSelectElement(id);
                        var  $data = {channelalias:element.channelalias,channeltype:element.channeltype};
                        live2VOD.Dao.delChannel($data, function (data) {
                            //{"code": 200, "meta": {}, "response": {"msg": "success", "code": "A00000"}}
                            if(data != null && data.code == 200){
                                if(data.response.code =="A00000" ){
                                    EpgWeb.Plugin.showTips({content:"success" });
                                    $("button[name=channelQuery]").click();
                                }else{
                                    EpgWeb.Plugin.showTips({type:"error",content:data.response.msg })
                                }
                            }else{
                                EpgWeb.Plugin.showTips({type:"error",content:"live2VOD delChannel has error" })
                            }
                        })
                    }
                }
            })
        },
        getSelectElement: function (elementId) {
            var selectElement = null;
            var jsonData = live2VOD.Config.PageBean.getCur();
            $.each(jsonData.rows, function (i, element) {
                if (element.id == elementId) {
                    selectElement = element;
                    return false;
                }
            })
            return selectElement;
        },
        jumpToEdit: function (id) {
            var currentUrl = window.location.toString();
            var index = currentUrl.indexOf("live2VOD_cp_home.html");
            var openUrl = currentUrl.substr(0, index) + "live2VOD_edit.html";
            if (id != null) {
                var element = live2VOD.Util.getSelectElement(id);
                openUrl += "?channelalias=" + element.channelalias + "&channeltype=" + element.channeltype;
            }
            window.open(openUrl);
        },
        getValidator: function($container, config) {
            // var $container = this.$container;
            var $form = $container;
            var validator = Validator.getValidator($form, config);

            return validator;
        }

    });
});